/*
 * Utils.cpp
 *
 *  Created on: 21/05/2014
 *      Author: lucio
 */

#include "SDLMainWindow.h"
#include "Timer.h"
#include "Utils.h"
//Screen attributes
const int SCREEN_BPP = 32;

//The frame rate
const int FRAMES_PER_SECOND = 20;

//The dot dimensions
const int DOT_WIDTH = 20;
const int DOT_HEIGHT = 20;

//The dimensions of the level
const int LEVEL_WIDTH = 1280;
const int LEVEL_HEIGHT = 960;

SDL_Surface* SDLMainWindow::load_image( std::string filename )
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized surface that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = IMG_Load(filename.c_str());

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized surface
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old surface
        SDL_FreeSurface( loadedImage );

        //If the surface was optimized
        if( optimizedImage != NULL )
        {
            //Color key surface
            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY,
            		SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
        }
    }

    //Return the optimized surface
    return optimizedImage;
}

bool SDLMainWindow::init()
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }

    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return false;
    }

    //Set the window caption
    SDL_WM_SetCaption( "Move the Dot", NULL );

    this->bomberman = new Bomberman(this->screen, this->camera);

    //If everything initialized fine
    return true;
}

bool SDLMainWindow::load_files()
{
    //Load the dot image
	this->bomberman->object = load_image("dot.bmp");

    //Load the background
    background = load_image("bg.png");

    //If there was a problem in loading the dot
    if(this->bomberman->object == NULL) {
        return false;
    }

    //If there was a problem in loading the background
    if( background == NULL )
    {
        return false;
    }

    //If everything loaded fine
    return true;
}

void SDLMainWindow::clean_up()
{
    //Free the surfaces
    //SDL_FreeSurface(&this->bomberman.getObject());
    SDL_FreeSurface(this->background);

    //Quit SDL
    SDL_Quit();
}

int SDLMainWindow::ExecuteWindow(){
    //Quit flag
    bool quit = false;

    //The frame rate regulator
    Timer fps;

    //Initialize
    if( init() == false )
    {
        return 1;
    }

    //Load the files
    if( load_files() == false )
    {
        return 1;
    }

    //While the user hasn't quit
    while( quit == false )
    {
        //Start the frame timer
        fps.start();

        //While there's events to handle
        while(SDL_PollEvent( &bomberman->event ))
        {
            //Handle events for the dot
            bomberman->handle_input();

            //If the user has Xed out the window
            if( bomberman->event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
            }
        }

        //Move the dot
        bomberman->move();

        //Set the camera
        bomberman->set_camera();

        //Show the background
        Utils::apply_surface( 0, 0, background, bomberman->screen, &bomberman->cam);

        //Show the dot on the screen
        bomberman->show();

        //Update the screen
        if( SDL_Flip( screen ) == -1 )
        {
            return 1;
        }

        //Cap the frame rate
        if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
        {
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
        }
    }

    //Clean up
    clean_up();

    return 0;
}

SDLMainWindow::SDLMainWindow() {
	this->background = NULL;
	this->screen = NULL;
	this->camera.x = 0;
	this->camera.y = 0;
	this->camera.w = SCREEN_WIDTH;
	this->camera.h = SCREEN_HEIGHT;
	this->bomberman = NULL;
}

SDLMainWindow::~SDLMainWindow() { }

